﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Publish_News : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = GetLatestNews(20);
        if (dt != null)
        {
            if (dt.Rows.Count < 10)
            {
                lstNews.DataSource = dt;
                lstNews.DataBind();
            }
            else
            {
                GssDO.DONews DO = new GssDO.DONews();
                DataTable dtNews = new DataTable();
                DataTable dtSubs = new DataTable();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    DataColumn dcNews = new DataColumn(dt.Columns[i].ColumnName, dt.Columns[i].DataType);
                    DataColumn dcSubs = new DataColumn(dt.Columns[i].ColumnName, dt.Columns[i].DataType);
                    dtNews.Columns.Add(dcNews);
                    dtSubs.Columns.Add(dcSubs);
                }
                DataRow[] subs = new DataRow[dt.Rows.Count - 10];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = null;
                    if (i < 10)
                    {
                        dr = dtNews.NewRow();
                        dr[DO.PKIDNewsColumnName] = dt.Rows[i][DO.PKIDNewsColumnName];
                        dr[DO.TitleColumnName] = dt.Rows[i][DO.TitleColumnName];
                        dr[DO.ShortTextColumnName] = dt.Rows[i][DO.ShortTextColumnName];
                        dr[DO.ImageColumnName] = dt.Rows[i][DO.ImageColumnName];
                        dr[DO.CreatedDateColumnName] = dt.Rows[i][DO.CreatedDateColumnName];
                        dtNews.Rows.Add(dr);
                    }
                    else
                    {
                        dr = dtSubs.NewRow();
                        dr[DO.PKIDNewsColumnName] = dt.Rows[i][DO.PKIDNewsColumnName];
                        dr[DO.TitleColumnName] = dt.Rows[i][DO.TitleColumnName];
                        dr[DO.CreatedDateColumnName] = dt.Rows[i][DO.CreatedDateColumnName];
                        dtSubs.Rows.Add(dr);
                    }
                }
                lstNews.DataSource = dtNews;
                lstNews.DataBind();
                if (dtSubs != null && dtSubs.Rows.Count > 0)
                {
                    rptNews.DataSource = dtSubs;
                    rptNews.DataBind();
                }
            }
        }
    }

    protected string BindNews(string id, string title, string shortText, string image, object datetime)
    {
        string rs = "";
        if (image != "")
        {
            rs += "<table align='left'><tr><td style='padding: 0px 6px 4px 0px;'><a href='NewsDetail.aspx?id=" + id + "'>";
            rs += "<img alt='' src='NewsThumbnail.aspx?id=" + id + "&width=120' border='0' align='left' onerror=\"this.src='../images/noimage90.gif'\" />";
            rs += "</a></td></tr></table>";
        }
        rs += "<p class='title'><a href='NewsDetail.aspx?id=" + id + "'>" + title + "</a> <span>(" + ((DateTime)datetime).ToString("yyyy/MM/dd") + ")</span></p>";
        rs += "<p>" + shortText + "</p>";
        rs += "<p class='detail'><a href='NewsDetail.aspx?id=" + id + "'>Chi ti&#7871;t</a></p>";
        return rs;
    }

    protected string BindNews(string id, string title, object datetime)
    {
        string rs = "";
        rs += "<a href='NewsDetail.aspx?id=" + id + "'>" + title + "</a> <span>(" + ((DateTime)datetime).ToString("yyyy/MM/dd") + ")</span>";
        return rs;
    }

    private DataTable GetLatestNews(int rows)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLNews BL = new GssBL.BLNews();
            dt = BL.getLatestNews(rows);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }
}
